package ink.yql.service.Impl;

import ink.yql.dao.AccountnumberDao;
import ink.yql.model.Accountnumber;
import ink.yql.model.User;
import ink.yql.service.AccountnumberService;
import ink.yql.util.RestObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@Transactional
public class AccountnumberServiceImpl implements AccountnumberService {
    @Autowired
    private AccountnumberDao accountnumberDao;
    @Autowired
    private HttpServletRequest req;
    @Override
    public RestObject getByUid() {
        // 获取session中的用户
        User user = (User) req.getSession().getAttribute("user");
        if (user ==null){
            return  RestObject.builder().code("404").message("请先登录").build();
        }
        // 通过一条用户id获取所有账号
        List<Accountnumber> list = accountnumberDao.getByUid(user.getUid());
        return RestObject.builder().code("200").message("查询成功").data(list).build();
    }

    @Override
    public RestObject getByUidDefault() {
        // 获取session中的用户
        User user = (User) req.getSession().getAttribute("user");
        if (user ==null){
            return  RestObject.builder().code("404").message("请先登录").build();
        }
        // 通过一条用户id获取默认账号
        Accountnumber accountnumber = accountnumberDao.getByUidDefault(user.getUid());
        return RestObject.builder().code("200").message("查询成功").data(accountnumber).build();
    }

    @Override
    public RestObject addOne(Accountnumber accountnumber) {
        // 获取登录用户id
        User user = (User) req.getSession().getAttribute("user");
        if (user ==null){
            return  RestObject.builder().code("404").message("请先登录").build();
        }
        // 校验数据格式
        if (accountnumber == null|| accountnumber.getAnaccountNumber() ==null){
            return RestObject.builder().code("400").message("请输入正确的数据").build();
        }
        // 将账号绑定到登录的用户上
        accountnumber.setUid(user.getUid());
        // 查看是否设置默认
        if (accountnumber.getAndefault() == null){
            accountnumber.setAndefault(0);
        }
        // 设置默认清空改用户的其他默认账号
        if(accountnumber.getAndefault() == 1){
            accountnumberDao.updateDefaultByUid(accountnumber.getUid());
        }
        // 将账号添加到用户的账号中
        accountnumberDao.addOne(accountnumber);
        return RestObject.builder().code("200").message("账号添加成功").build();
    }

    @Override
    public RestObject update(Accountnumber accountnumber) {
        // 获取登录用户id
        User user = (User) req.getSession().getAttribute("user");
        if (user ==null){
            return  RestObject.builder().code("404").message("请先登录").build();
        }
        // 校验数据格式
        if (accountnumber == null|| accountnumber.getAnaccountNumber() ==null){
            return RestObject.builder().code("400").message("请输入正确的数据").build();
        }
        // 设置默认清空改用户的其他默认账号
        if(accountnumber.getAndefault() == 1){
            accountnumberDao.updateDefaultByUid(accountnumber.getUid());
        }
        // 将更新该账号的信息
        accountnumberDao.update(accountnumber);
        return RestObject.builder().code("200").message("账号修改成功").build();
    }

    @Override
    public RestObject deleteByAnid(Integer anid) {
        // 查看是否登录
        User user = (User) req.getSession().getAttribute("user");
        if (user ==null){
            return  RestObject.builder().code("404").message("请先登录").build();
        }
        // 校验数据
        if (anid == null){
            return RestObject.builder().code("400").message("请输入正确的数据").build();
        }
        // 查看是否有该账号
        List<Accountnumber> list = accountnumberDao.getByUid(user.getUid());
        list.forEach(acc ->{
            // 如果有就删除
            if (acc.getAnid().equals(anid)){
                accountnumberDao.delete(anid);
            }
        });
        return RestObject.builder().code("200").message("账号删除成功").build();
    }
}
